package com.happyfamily.springboot.crowdsourcingplatform.controller.guo;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.*;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FriendService;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FrontNeedService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller
public class FriendShipController
{
	@Autowired
	private FriendService friendService;

	@Autowired
	private FrontNeedService frontNeedService;

	@GetMapping("/api-back/friend/friendList")
	@ResponseBody
	@ApiOperation(value = "后端显示友情链接列表", notes = "后端显示友情链接列表")
	@OperationLogDetail(detail = "后端显示友情链接列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public TableModel getFriendByPage(HttpServletRequest request, String page, String limit, String ftitle)
	{
		int page1 = Integer.valueOf(page);
		int limit1 = Integer.valueOf(limit);
		RowBounds rowBounds = new RowBounds((page1 - 1) * limit1, limit1);
		if (ftitle != null)
		{
			ftitle = "%" + ftitle + "%";
		} else
		{
			ftitle = "%%";
		}
		List<FriendshipLink> list = friendService.searchFriend(rowBounds, ftitle);

		TableModel tableModel = new TableModel();

		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(friendService.totalFriend(ftitle));
		tableModel.setData(list);
		return tableModel;

	}

	@GetMapping("/friend/friendListFront")
	@ResponseBody
	@ApiOperation(value = "前端获取友情链接集合", notes = "前端获取友情链接集合")
	@OperationLogDetail(detail = "前端获取友情链接集合", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<FriendshipLink> getFrontFriendShip()
	{
		return friendService.getFrontFriendShip();

	}

	@PreAuthorize("hasAuthority('back:friendlink:update')")
	@GetMapping("/api-back/friend/changeTypeBuFid")
	@ResponseBody
	@ApiOperation(value = "后端更改友情链接状态", notes = "后端更改友情链接状态")
	@OperationLogDetail(detail = "后端更改友情链接状态", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String changeTypeBuFid(String fid){
		return friendService.changeTypeBuFid(Long.valueOf(fid));
	};

	@PreAuthorize("hasAuthority('back:friendlink:update')")
	@GetMapping("/api-back/friend/changeTypeBuFidClose")
	@ResponseBody
	@ApiOperation(value = "后端更改友情链接状态", notes = "后端更改友情链接状态")
	@OperationLogDetail(detail = "后端更改友情链接状态", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String changeTypeBuFidClose(String fid){
		return friendService.changeTypeBuFidClose(Long.valueOf(fid));
	};

	@PreAuthorize("hasAuthority('back:friendlink:delete')")
	@GetMapping("/api-back/friend/changeTypeBuFidDelete")
	@ResponseBody
	@ApiOperation(value = "后端删除友情链接", notes = "后端删除友情链接")
	@OperationLogDetail(detail = "后端删除友情链接", level = 4, operationUnit = OperationUnit.USER, operationType = OperationType.DELETE)
	public String changeTypeBuFidDelete(String fid){
		return friendService.changeTypeBuFidDelete(Long.valueOf(fid));
	};

	@GetMapping("/api-back/friend/getInfoFriendLink")
	@ResponseBody
	@ApiOperation(value = "后端获取友情链接对象", notes = "后端获取友情链接对象")
	@OperationLogDetail(detail = "后端获取友情链接对象", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public FriendshipLink getInfoFriendLink(String fid){
		return friendService.getInfoFriendLink(Long.valueOf(fid));
	};

	@PreAuthorize("hasAuthority('back:friendlink:delete')")
	@GetMapping("/api-back/friend/changeTypeBuFidUpdate")
	@ResponseBody
	@ApiOperation(value = "后端更改友情链接", notes = "后端更改友情链接")
	@OperationLogDetail(detail = "后端更改友情链接", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String changeTypeBuFidUpdate(String fid,String furl,String ftitle){
		return friendService.changeTypeBuFidUpdate(ftitle,furl,Long.valueOf(fid));
	};

	@GetMapping("/friend/getCounltantUsder")
	@ResponseBody
	@ApiOperation(value = "前端获取顾问展示", notes = "前端获取顾问展示")
	@OperationLogDetail(detail = "前端获取顾问展示", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<SysUser> getCounltantUsder()
	{
		List<SysUser> users=frontNeedService.getCounltantUsder(Long.valueOf(7));
		for (int i = 0; i <users.size() ; i++)
		{
			users.get(i).setTips1(frontNeedService.getType(Long.valueOf(users.get(i).getSid())).getSname());
		}



		return users;
	}
}
